package whf.framework.report;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import whf.framework.log.Log;
import whf.framework.log.LogFactory;
import whf.framework.util.StringUtils;

/**
 * @author King
 * @create Jan 18, 2008 5:07:36 PM
 */
@SuppressWarnings("unchecked")
public class ReportExportFilter implements Filter {
	private static Log log = LogFactory.getLog(ReportExportFilter.class);

	private String contentDisposition;

	private String pragma;

	private long expires;

	private ReportExportHandler reportExportHandler;

	public void init(FilterConfig config) throws ServletException {
		this.contentDisposition = config.getInitParameter("Content-Disposition");
		if (StringUtils.isEmpty(this.contentDisposition)) {
			this.contentDisposition = "attachment";
		}
		this.pragma = config.getInitParameter("Pragma");
		if (StringUtils.isEmpty(this.pragma)) {
			this.pragma = "public";
		}
		try {
			this.expires = Long.parseLong(config.getInitParameter("Expires"));
		} catch (Exception e) {
			this.expires = 1000;
		}
		try{
			Class cls = Class.forName(config.getInitParameter("reportExportHandlerClassName"));
			this.reportExportHandler = (ReportExportHandler)cls.newInstance();
			this.reportExportHandler.init(config.getServletContext());
		} catch(Exception e){
			log.error(this, e);
			throw new ServletException(e);
		}
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

		if (ReportUtils.isExportMode(request)) {
			String exportName = request.getParameter(ReportConstants.EXPORT_FILE_NAME);
			GenericResponseWrapper wrapper = new GenericResponseWrapper((HttpServletResponse) response);
			//
			chain.doFilter(request, wrapper); // 主要用于获取数据
			
			// 准备输出报表
			setResponseHeaders((HttpServletResponse) response, exportName);
			try {
				HttpServletRequest req = (HttpServletRequest) request;
				HttpServletResponse res = (HttpServletResponse) response;
				this.reportExportHandler.handle(req, res);
			} catch (Exception e) {
				log.error(this, e);
				throw new ServletException(e);
			}
		} else {
			chain.doFilter(request, response);
		}
	}

	public void destroy() {

	}

	protected void setResponseHeaders(HttpServletResponse response, String exportName) {
		String mimeType = ReportConstants.getFileMimeType(exportName);
		if (!StringUtils.isEmpty(mimeType)) {
			response.setContentType(mimeType);
		}
		response.setHeader("Content-Disposition", this.contentDisposition + ";filename=\"" + exportName + "\"");
		response.setHeader("Pragma", this.pragma);
		response.setDateHeader("Expires", (System.currentTimeMillis() + this.expires));
	}
}